﻿using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
//using System.Web;

namespace PubSubHubbub
{
	public enum VerifyType
	{
		Async,
		Sync
	}

	public enum SubscriberMode
	{
		Subscribe,
		Unsubscribe
	}

	public class SubscriberClient
	{
		public Uri SubscribeHub
		{
			get { return _subscribeHub; }
			set { _subscribeHub = value; }
		}

		public Uri CallbackUrl
		{
			get { return _callbackUrl; }
			set { _callbackUrl = value; }
		}

		public VerifyType VerifyType
		{
			get { return _verifyType; }
			set { _verifyType = value; }
		}

		public SubscriberMode Mode
		{
			get { return _mode; }
			set { _mode = value; }
		}

		public string Topic
		{
			get { return _topic; }
			set { _topic = value; }
		}

		public string VerifyToken
		{
			get { return _verifyToken; }
			set { _verifyToken = value; }
		}

		public string Secret
		{
			get { return _secret; }
			set { _secret = value; }
		}

		protected SubscriberClient()
		{

		}

		public SubscriberClient(Uri callbackUri, string topic)
			: this()
		{
			_callbackUrl = callbackUri;
			_topic = topic;
		}

		public void Subscribe()
		{
			using (WebClient client = new WebClient())
			{
				var nvc = new NameValueCollection();
				nvc.Add("hub.callback", _callbackUrl.AbsoluteUri);
				nvc.Add("hub.topic", _topic);
				nvc.Add("hub.verify", _verifyType.ToString().ToLower());
				nvc.Add("hub.mode", _mode.ToString().ToLower());
				nvc.Add("hub.verify_token", _verifyToken);
				nvc.Add("hub.secret", _secret);

				byte[] results = client.UploadValues(this._subscribeHub.AbsoluteUri, "POST", nvc);
			}
		}

		private Uri _subscribeHub = new Uri("http://pubsubhubbub.appspot.com/subscribe");
		private Uri _callbackUrl = null;
		private VerifyType _verifyType = VerifyType.Async;
		private SubscriberMode _mode = SubscriberMode.Subscribe;
		private string _topic = "";
		private string _verifyToken = "";
		private string _secret = "";
	}
}
